/**
 * 后台功能
 * */


(function () {
})();


//获取功能列表
function getList(){
    var adminInfo = getAdminInfo();
    if (adminInfo.role == 2) {
        $('#addOut').show();
    }
    $.ajax({
        type: apis.adminIndex.method,
        url: apis.adminIndex.url,
        data: { token: adminInfo.token },
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//HTML
function getHtml(d){
    var adminInfo = getAdminInfo();
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].name + '</td>';
        h += '<td>' + d[i].role_name + '管理员</td>';
        h += '<td>' + d[i].able_name + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'admin-edit.html\', ' + d[i].id + ')">';
        h += '编辑';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="lay_pwd(\'改密码\',\'admin-pwd.html\', ' + d[i].id + ')">';
        h += '改密码';
        h += '</a>';
        if (adminInfo.role == 2) {
            h += '<br>';
            h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ')">';
            h += '删除';
            h += '</a>';
            h += ' &nbsp; ';
            if (!d[i].disable) {
                h += '<a href="javascript:;" style="color: red;" onclick="able_no(' + d[i].id + ')">';
                h += '禁用';
                h += '</a>';
            } else {
                h += '<a href="javascript:;" style="color: green;" onclick="able_yes(' + d[i].id + ')">';
                h += '启用';
                h += '</a>';
            }
        }
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//获取一个
function getOne(){
    if (!id) { return }
    $.ajax({
        type: apis.adminOne.method,
        url: apis.adminOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            $('#title')[0].value = res.data.name;
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,300);
}


//密码
function lay_pwd(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,300);
}


//修改
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
    };
    if (!formData.title) {
        msgPop({msg: '手机号必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.adminModify.method,
        url: apis.adminModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//更新密码
function setPwd(){
    var formData = {
        id: id,
        pwdold: $('#pwdold').val(),
        pwdnew: $('#pwdnew').val(),
    };
    if (!formData.id) {
        msgPop({msg: 'ID必须的'}); return
    }
    if (!formData.pwdold) {
        msgPop({msg: '密码必须的'}); return
    }
    if (!formData.pwdnew) {
        msgPop({msg: '新密码必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.adminPwd.method,
        url: apis.adminPwd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function lay_del(v){
    layer.confirm('确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？',
        function(index){
        $.ajax({
            type: apis.adminDel.method,
            url: apis.adminDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//禁用
function able_no(v){
    layer.confirm('确认要 <b style="color: red; font-size: 20px;">禁用</b> 吗？',
        function(index){
            able_api({id: v, able: 1});
    });
}
//启用
function able_yes(v){
    layer.confirm('确认要 <b style="color: red; font-size: 20px;">启用</b> 吗？',
        function(index){
            able_api({id: v, able: 0});
    });
}
//ableAPI
function able_api(e){
    $.ajax({
        type: apis.adminAble.method,
        url: apis.adminAble.url,
        data: { id: e.id, able: e.able },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            layerMsgOk('操作成功!');
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}